package com.ny.zmb.openapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmRegister;
import com.ny.zmb.openapi.entity.PmRegisterExamine;
import com.ny.zmb.openapi.mapper.PmRegisterExamineMapper;
import com.ny.zmb.openapi.mapper.PmRegisterMapper;
import com.ny.zmb.openapi.service.PmRegisterExamineService;
import com.ny.zmb.openapi.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PmRegisterExamineServiceImpl extends ServiceImpl<PmRegisterExamineMapper, PmRegisterExamine> implements PmRegisterExamineService {
    @Resource
    private PmRegisterExamineMapper pmRegisterExamineMapper;
    @Resource
    private PmRegisterMapper pmRegisterMapper;
    private QueryWrapper<PmRegisterExamine> queryWrapper = new QueryWrapper<PmRegisterExamine>();
    private QueryWrapper<PmRegister> pmRegisterQueryWrapper = new QueryWrapper<PmRegister>();

    public PmRegisterExamine getByOutMerchantNo(String outMerchantNo) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_merchant_no", outMerchantNo);
        return pmRegisterExamineMapper.selectOne(queryWrapper);
    }

    /**
     * 审核资料状态变更
     *
     * @param obj
     * @return
     */
    public Result changeStatus(JSONObject obj) {
        String outMerchantNo = obj.getString("outMerchantNo");
        PmRegisterExamine pmRegisterExamine = getByOutMerchantNo(outMerchantNo);
        if (pmRegisterExamine == null) {
            return Result.fail("没有找到对应审核记录");
        }
        int status = obj.getInteger("status");
        String msg = obj.getString("msg");
        pmRegisterExamine.updateZMB(status, msg);
        boolean b = updateById(pmRegisterExamine);
        return b ? Result.ok("更新成功", pmRegisterExamine) : Result.fail("更新失败");
    }

    @Override
    public Integer UpdateBymMerchantNo(JSONObject obj) {
        String state = obj.getString("state");
        String contractSn = obj.getString("contractSn");
        String merchantNo = obj.getString("merchantNo");
        String successTime = obj.getString("successTime");
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_no", merchantNo);
        PmRegisterExamine pmRegisterExamine = pmRegisterExamineMapper.selectOne(queryWrapper);
        if (pmRegisterExamine != null) {
            switch (state) {
                case "success":
                case "SUCCESS":
                    pmRegisterExamine.updateYS(1, obj.toString());
                    break;
                case "failed":
                case "FAILED":
                    pmRegisterExamine.updateYS(-2, obj.toString());
                    break;
                default:
                    pmRegisterExamine.updateYS(999, obj.toString());
                    break;
            }
            pmRegisterExamine.setUpdatedDate(DateUtil.DateConversion("yyyyMMddHHmmss", successTime));
            return pmRegisterExamineMapper.updateById(pmRegisterExamine);
        }
        return -1;

    }

    @Override
    public Integer updateMerchantNoByOutMerchantNo(String outMerchantNo, String merchantNo) {
        PmRegisterExamine pmRegisterExamine = getByOutMerchantNo(outMerchantNo);
        if (pmRegisterExamine != null) {
            pmRegisterExamine.setMerchantNo(merchantNo);
            return pmRegisterExamineMapper.updateById(pmRegisterExamine);
        } else {
            return -1;
        }
    }

    @Override
    public PmRegisterExamine getInfoByUserId(Integer userId) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return pmRegisterExamineMapper.selectOne(queryWrapper);
    }
}
